package com.cat.binaryAnswer;
/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/description/
 * @create 2025/8/9 18:49
 * @since JDK17
 */

public class Solution01 {
    int f1(int[] nums, int target) {
        int l = -1, r = nums.length - 1, mid;
        while (l + 1 < r) {
            mid = l + (r - l) / 2;
            if (nums[mid] >= target) {  // 大了
                r = mid;
            } else {
                l = mid;
            }
        }
        return nums[r] != target ? -1 : r;
    }

    int f2(int[] nums, int target) {
        int l = 0, r = nums.length, mid;
        while (l + 1 < r) {
            mid = l + (r - l) / 2;
            if (nums[mid] <= target) {
                l = mid;
            } else {
                r = mid;
            }
        }
        return nums[l] == target ? l : -1;
    }

    public int[] searchRange(int[] nums, int target) {
        if (nums.length == 0) {
            return new int[]{-1, -1};
        }
        int[] ans = new int[2];
        ans[0] = f1(nums, target);
        ans[1] = f2(nums, target);
        return ans;
    }
}
